package com.spider.admin.logs.service;


import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import com.jayway.jsonpath.JsonPath;

import java.util.List;

public class LogSettingTransferUtil {

    public static final String HEADER = "[headers]";
    public static final String PATH = "[path]";
    public static final String PARAMS = "[params]";
    public static final String BODY = "[body]";
    public static final String STATIC = "[static]";

    /**
     * 请求头数据转换
     *
     * @param source
     * @param setting
     * @return
     */
    public static final String transForHeaders(String source, String setting) {
        String value = null;
        try {
            if (StringUtils.isNotEmpty(source) && StringUtils.isNotEmpty(setting)) {
                setting = setting.replace(HEADER, "");
                if (StringUtils.isNotEmpty(setting)) {
                    setting = setting.substring(1, setting.length() - 1);
                    if (!setting.startsWith("$.")) {
                        setting = "$." + setting;
                    }
                    Object obj = JsonPath.read(source, setting);
                    value = String.valueOf(obj);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return value;
    }

    /**
     * 请求路径数据转换
     *
     * @param source
     * @param setting
     * @return
     */
    public static final String transForPath(String source, String setting) {
        String value = null;
        try {
            if (StringUtils.isNotEmpty(source) && StringUtils.isNotEmpty(setting)) {
                String uriSections[] = source.split(" ")[1].split("/");
                setting = setting.replace(PATH, "").replace("[", "").replace("]", "");

                if (StringUtils.isNotEmpty(setting)) {
                    int index = Integer.parseInt(setting);
                    if (index <= uriSections.length) {
                        value = uriSections[index];
                        int i = value.indexOf("?");
                        if (i >= 0) {
                            value = value.substring(0, i);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return value;
    }

    /**
     * 请求参数数据转换
     *
     * @param source
     * @param setting
     * @return
     */
    public static final String transForParams(String source, String setting) {

        String value = null;
        try {
            if (StringUtils.isNotEmpty(source) && StringUtils.isNotEmpty(setting)) {
                setting = setting.replace(PARAMS, "").replace("[", "").replace("]", "");
                String[] params = source.split("&");
                if (params != null && params.length > 0) {
                    for (String ts : params) {
                        if (ts.startsWith(setting)) {
                            String[] ps = ts.split("=");
                            if (ps != null && ps.length == 2) {
                                value = StringUtils.trim(ps[1]);
                                break;
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }


    /**
     * 请求体数据转换
     *
     * @param source
     * @param setting
     * @return
     */
    public static final String transForBody(String source, String setting) {
        String value = null;
        try {
            if (StringUtils.isNotEmpty(source) && StringUtils.isNotEmpty(setting)) {
                setting = setting.replace(BODY, "");
                if (StringUtils.isNotEmpty(setting)) {
                    setting = setting.substring(1, setting.length() - 1);
                    if (!setting.startsWith("$.")) {
                        setting = "$." + setting;
                    }
                    Object obj = JsonPath.read(source, setting);
                    value = String.valueOf(obj);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return value;
    }

    /**
     * 静态数据转换
     *
     * @param setting
     * @return
     */
    public static final String transForStatic(String setting) {
        String value = null;
        if (StringUtils.isNotEmpty(setting)) {
            value = setting.replace(STATIC, "").replace("[", "").replace("]", "");
        }
        return value;
    }
}
